package com.gemcoder.system.service;
import static com.gemcoder.system.datasource.JdbcTemplateUtil.executeSQL;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import static com.gemcoder.system.service.UserSdkService.sdkOfGetCurrentUserName;
import static com.gemcoder.system.service.UserSdkService.sdkOfGetCurrentUserId;
import com.gemcoder.system.bo.Result;
import static com.gemcoder.system.service.permission.PermissionSdkService.sdkOfgetAllPermissions;
import static com.gemcoder.system.service.permission.PermissionSdkService.sdkOfgetAllMenus;
import java.sql.SQLException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import cn.hutool.core.util.StrUtil;

@Service
public class StabilityofcompanyoperationService {

    public Result getCompanyOperationalStabilities(Map<String,Object> paramMap){
    String projectName = paramMap.get("projectName") != null ? String.valueOf(paramMap.get("projectName")) : null;
    Integer current = paramMap.get("current") != null ? Integer.valueOf(String.valueOf(paramMap.get("current"))) : 1;
    Integer pageSize = paramMap.get("pageSize") != null ? Integer.valueOf(String.valueOf(paramMap.get("pageSize"))) : 10;

    StringBuilder sql = new StringBuilder("SELECT id, project_name, water_quality_days, stable_operation_days, safe_production_days FROM company_operational_stability WHERE 1=1");
    List<Object> params = new ArrayList<>();

    if (projectName != null && !projectName.isEmpty()) {
        sql.append(" AND project_name LIKE ?");
        params.add("%" + projectName + "%");
    }

    sql.append(" LIMIT ?, ?");
    params.add((current - 1) * pageSize);
    params.add(pageSize);

    List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());

    StringBuilder countSql = new StringBuilder("SELECT COUNT(*) as total FROM company_operational_stability WHERE 1=1");
    List<Object> countParams = new ArrayList<>();
    if (projectName != null && !projectName.isEmpty()) {
        countSql.append(" AND project_name LIKE ?");
        countParams.add("%" + projectName + "%");
    }
    List<Map<String, Object>> countResult = executeSQL(countSql.toString(), countParams.toArray());
    int total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;

    Map<String, Object> result = new HashMap<>();
    result.put("total", total);
    result.put("data", data);

    return new Result(true, "查询成功", result);
}

public Result getAllCompanyOperationalStabilities(Map<String,Object> paramMap){
    Integer current = paramMap.get("current") != null ? Integer.valueOf(String.valueOf(paramMap.get("current"))) : 1;
    Integer pageSize = paramMap.get("pageSize") != null ? Integer.valueOf(String.valueOf(paramMap.get("pageSize"))) : 10;

    StringBuilder sql = new StringBuilder("SELECT id, project_name, water_quality_days, stable_operation_days, safe_production_days FROM company_operational_stability LIMIT ?, ?");
    List<Object> params = new ArrayList<>();
    params.add((current - 1) * pageSize);
    params.add(pageSize);

    List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());

    StringBuilder countSql = new StringBuilder("SELECT COUNT(*) as total FROM company_operational_stability");
    List<Map<String, Object>> countResult = executeSQL(countSql.toString());
    int total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;

    Map<String, Object> result = new HashMap<>();
    result.put("total", total);
    result.put("data", data);

    return new Result(true, "查询成功", result);
}

public Result getCompanyOperationalStabilitiesByPage(Map<String,Object> paramMap){
    Integer current = paramMap.get("current") != null ? Integer.valueOf(String.valueOf(paramMap.get("current"))) : 1;
    Integer pageSize = paramMap.get("pageSize") != null ? Integer.valueOf(String.valueOf(paramMap.get("pageSize"))) : 10;

    StringBuilder sql = new StringBuilder("SELECT id, project_name, water_quality_days, stable_operation_days, safe_production_days FROM company_operational_stability LIMIT ?, ?");
    List<Object> params = new ArrayList<>();
    params.add((current - 1) * pageSize);
    params.add(pageSize);

    List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());

    StringBuilder countSql = new StringBuilder("SELECT COUNT(*) as total FROM company_operational_stability");
    List<Map<String, Object>> countResult = executeSQL(countSql.toString());
    int total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;

    Map<String, Object> result = new HashMap<>();
    result.put("total", total);
    result.put("data", data);

    return new Result(true, "查询成功", result);
}

public Result addCompanyOperationalStability(Map<String,Object> paramMap){
    String projectName = paramMap.get("ProjectName1value2") != null ? String.valueOf(paramMap.get("ProjectName1value2")) : null;
    Integer waterQualityDays = paramMap.get("Daysofwaterqualityuptostandardvalue1") != null ? Integer.valueOf(String.valueOf(paramMap.get("Daysofwaterqualityuptostandardvalue1"))) : null;
    Integer stableOperationDays = paramMap.get("Stableoperationdaysvalue1") != null ? Integer.valueOf(String.valueOf(paramMap.get("Stableoperationdaysvalue1"))) : null;
    Integer safeProductionDays = paramMap.get("Safetyproductiondaysvalue1") != null ? Integer.valueOf(String.valueOf(paramMap.get("Safetyproductiondaysvalue1"))) : null;
    String createUsername = sdkOfGetCurrentUserName();

    StringBuilder sql = new StringBuilder("INSERT INTO company_operational_stability (project_name, water_quality_days, stable_operation_days, safe_production_days, create_username, create_time) VALUES (?, ?, ?, ?, ?, NOW())");
    List<Object> params = new ArrayList<>();
    params.add(projectName);
    params.add(waterQualityDays);
    params.add(stableOperationDays);
    params.add(safeProductionDays);
    params.add(createUsername);

    int rows = executeSQL(sql.toString(), params.toArray());

    if (rows > 0) {
        return new Result(true, "新增成功", null);
    } else {
        return new Result(false, "新增失败", null);
    }
}

public Result updateCompanyOperationalStability(Map<String,Object> paramMap){
    Integer id = paramMap.get("id") != null ? Integer.valueOf(String.valueOf(paramMap.get("id"))) : null;
    String projectName = paramMap.get("ProjectNamevalue2") != null ? String.valueOf(paramMap.get("ProjectNamevalue2")) : null;
    Integer waterQualityDays = paramMap.get("Daysofwaterqualityuptostandardvalue2") != null ? Integer.valueOf(String.valueOf(paramMap.get("Daysofwaterqualityuptostandardvalue2"))) : null;
    Integer stableOperationDays = paramMap.get("Stableoperationdaysvalue2") != null ? Integer.valueOf(String.valueOf(paramMap.get("Stableoperationdaysvalue2"))) : null;
    Integer safeProductionDays = paramMap.get("Safetyproductiondaysvalue2") != null ? Integer.valueOf(String.valueOf(paramMap.get("Safetyproductiondaysvalue2"))) : null;
    String updateUsername = sdkOfGetCurrentUserName();

    StringBuilder sql = new StringBuilder("UPDATE company_operational_stability SET project_name = ?, water_quality_days = ?, stable_operation_days = ?, safe_production_days = ?, update_username = ?, update_time = NOW() WHERE id = ?");
    List<Object> params = new ArrayList<>();
    params.add(projectName);
    params.add(waterQualityDays);
    params.add(stableOperationDays);
    params.add(safeProductionDays);
    params.add(updateUsername);
    params.add(id);

    int rows = executeSQL(sql.toString(), params.toArray());

    if (rows > 0) {
        return new Result(true, "更新成功", null);
    } else {
        return new Result(false, "更新失败", null);
    }
}

public Result deleteCompanyOperationalStability(Map<String,Object> paramMap){
    Integer id = paramMap.get("id") != null ? Integer.valueOf(String.valueOf(paramMap.get("id"))) : null;

    StringBuilder sql = new StringBuilder("DELETE FROM company_operational_stability WHERE id = ?");
    List<Object> params = new ArrayList<>();
    params.add(id);

    int rows = executeSQL(sql.toString(), params.toArray());

    if (rows > 0) {
        return new Result(true, "删除成功", null);
    } else {
        return new Result(false, "删除失败", null);
    }
}

}